package com.sleepless.my.controller;

import com.sleepless.my.config.CustomUserDetails;
import com.sleepless.my.config.JwtUserDetailService;
import com.sleepless.my.io.RestBean;
import com.sleepless.my.io.request.Video.CreateVideoReq;
import com.sleepless.my.io.request.Video.GetVideosReq;
import com.sleepless.my.service.VideoService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/api/v1/video",produces = "application/json;charset=UTF-8")
@Slf4j
public class VideoController {
    @Resource
    private VideoService videoService;

    @PostMapping("/")
    Object createVideo(@RequestParam("file") MultipartFile file,@ModelAttribute CreateVideoReq req, @AuthenticationPrincipal CustomUserDetails user){
        log.debug("{}",req);
        if(req.getTitle()==null){
            return RestBean.failure(RestBean.Code.视频标题不能为空);
        }
        if(req.getDescription()==null){
            return RestBean.failure(RestBean.Code.视频描述不能为空);
        }
        if(req.getVideoType()==null){
            return RestBean.failure(RestBean.Code.视频类型不能为空);
        }
        req.setUuid(user.getUuid());
        Object resp=videoService.createVideo(file,req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
    @GetMapping("/random")
    Object getRandomVideo(){
        Object resp=videoService.getRandomVideo();
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
    @GetMapping("/{videoId}")
    Object getVideo(@PathVariable int videoId){
        Object resp=videoService.getVideo(videoId);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        log.debug("{}",resp);
        return RestBean.success(resp);
    }
    @PostMapping("/list")
    Object getVideos(@RequestBody GetVideosReq req){
        Object resp=videoService.getVideos(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
}
